package com.market.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "purchasedetail")
public class PurchaseDetail extends BaseObject {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7613437902927347133L;

	@Transient
	private Long newInventory =0L; // The inventory after purchase
	
	public Long getNewInventory() {
		return this.purchasedQuantity*this.product.getPurchaseToPriceSpec() + this.product.getInventory();
	}

	public void setNewInventory(Long newInventory) {
		this.newInventory = newInventory;
	}


	@Override
	public boolean equalsId(Object o) {
		if ((o instanceof PurchaseDetail)
				&& ((Purchase) o).getId().equals(this.id)) {
			return true;
		}
		return false;
	}
	
	@Transient
	public Double getPurchasedSubTotal() {
		return this.purchasedQuantity * this.purchasedPrice;
	}
	
	@Transient
	public Double getReceivedSubTotal() {
		return this.receivedQuantity * this.receivedPrice;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id = 0L;
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "product_id", nullable = false)
	private Product product;
	@Column(columnDefinition = "Integer Default 0")
	private Integer purchasedQuantity =0;
	@Column(columnDefinition = "Double Default 0")
	private Double purchasedPrice = 0D;
	@Column(columnDefinition = "Integer Default 0")
	private Integer receivedQuantity=0 ;
	@Column(columnDefinition = "Double Default 0")
	private Double receivedPrice = 0D;

	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Integer getPurchasedQuantity() {
		return purchasedQuantity;
	}

	public void setPurchasedQuantity(Integer purchasedQuantity) {
		this.purchasedQuantity = purchasedQuantity;
	}

	public Double getPurchasedPrice() {
		return purchasedPrice;
	}

	public void setPurchasedPrice(Double purchasedPrice) {
		this.purchasedPrice = purchasedPrice;
	}

	public Integer getReceivedQuantity() {
		return receivedQuantity;
	}

	public void setReceivedQuantity(Integer receivedQuantity) {
		this.receivedQuantity = receivedQuantity;
	}

	public Double getReceivedPrice() {
		return receivedPrice;
	}

	public void setReceivedPrice(Double receivedPrice) {
		this.receivedPrice = receivedPrice;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((receivedPrice == null) ? 0 : receivedPrice.hashCode());
		result = prime * result
				+ ((receivedQuantity == null) ? 0 : receivedQuantity.hashCode());
		result = prime * result
				+ ((newInventory == null) ? 0 : newInventory.hashCode());
		result = prime * result
				+ ((purchasedPrice == null) ? 0 : purchasedPrice.hashCode());
		result = prime
				* result
				+ ((purchasedQuantity == null) ? 0 : purchasedQuantity
						.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PurchaseDetail other = (PurchaseDetail) obj;
		if (receivedPrice == null) {
			if (other.receivedPrice != null)
				return false;
		} else if (!receivedPrice.equals(other.receivedPrice))
			return false;
		if (receivedQuantity == null) {
			if (other.receivedQuantity != null)
				return false;
		} else if (!receivedQuantity.equals(other.receivedQuantity))
			return false;
		if (newInventory == null) {
			if (other.newInventory != null)
				return false;
		} else if (!newInventory.equals(other.newInventory))
			return false;
		if (purchasedPrice == null) {
			if (other.purchasedPrice != null)
				return false;
		} else if (!purchasedPrice.equals(other.purchasedPrice))
			return false;
		if (purchasedQuantity == null) {
			if (other.purchasedQuantity != null)
				return false;
		} else if (!purchasedQuantity.equals(other.purchasedQuantity))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "PurchaseDetail [newInventory=" + newInventory + ", id=" + id
				+ ", purchasedQuantity=" + purchasedQuantity
				+ ", purchasedPrice=" + purchasedPrice + ", arrivedQuantity="
				+ receivedQuantity + ", arrivedPrice=" + receivedPrice + "]";
	}

	
}
